<?php
function fw_util_fetchPostValue($key = '')
{
	return $_POST[$key];
}

function fw_util_fetchGetValue($key = '')
{
	return $_GET[$key];
}

function fw_util_makeSessionKey()
{
	$s_userAgent = $_SERVER['HTTP_USER_AGENT'];
	$s_timestamp = date("Y-m-d H:i:s:ss");
	$s_first_key = substr($s_timestamp, rand(2,9), rand(2,9));
	$s_second_key = substr($s_userAgent, rand(2,9), rand(2,9));
	$s_third_key = substr($s_timestamp , rand(1,8), rand(1,8));
	$s_fourth_key = substr($s_userAgent , rand(1,8), rand(1,8));
	$s_concat_Key = $s_first_key.$s_second_key.$s_third_key.$s_fourth_key;
	$s_sessionKey = sha1(hash('md5',$s_concat_Key));
	return $s_sessionKey;
}

function fw_util_encrypt($s_rawString, $i_subCount = 20)
{
	$s_salt = 'saltedString';
	$s_rawEncryption = hash('md5', $s_salt . $s_rawString . $s_salt);
	$s_finalEncryption = substr($s_rawEncryption, 0, $i_subCount);
	return $s_finalEncryption;
}

function fw_util_outputJson($arr_inputArr)
{
	require C_FW_fwPackage.'utilities/json/JSON.php';
	header('Cache-Control: no-cache, must-revalidate');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Content-type: application/json');
	die(json_encode($arr_inputArr));
}

function fw_util_decodeJson($arr_inputJson)
{
	require C_FW_fwPackage.'utilities/json/JSON.php';
	return json_decode($arr_inputJson);
}
//new functions

function fw_util_findArrValue($arr_hayStack, $s_needle)
{
	$s_arrValue = '';
	
	if(isset($arr_hayStack) && !empty($arr_hayStack))
	{
		$i_arrCount = count($arr_hayStack);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_inHayStack = $arr_hayStack[$i];
			$s_arrFlag = array_search($s_needle, $arr_inHayStack, true);
				
			if($s_arrFlag !== false)
			{
				$s_arrValue = $arr_inHayStack;
			}
				
		}
	}
	return $s_arrValue;
}


function fw_util_getjsLoadFunction()
{
	$s_jsLoadFunction = '';
	include C_FW_customPackage.'configs/applications.php';
	if(isset($arr_applications) && !empty($arr_applications))
	{
		$i_arrCount = count($arr_applications);
		for($i=0;$i<$i_arrCount;$i++)
		{
			$arr_app = $arr_applications[$i];
			if(isset(Claycode_Router::getInstance()->s_currAppIdent)){
				$s_arrKey = array_search(Claycode_Router::getInstance()->s_currAppIdent, $arr_app, true);

				if($s_arrKey !== false)
				{
					$s_jsLoadFunction = $arr_app['js_load_function'];
						
				}
			}

		}
	}
	return $s_jsLoadFunction;
}

function fw_util_arrayPush($arr_base, $o_input){
	if(is_array($o_input) == true){
		$arr_base = array_merge($arr_base, $o_input);
	}else{
		array_push($arr_base, $o_input);
	}
	return $arr_base;
}

function fw_util_getCurrUrl() 
{
	// output: /myproject/index.php
	$currentPath = $_SERVER['PHP_SELF'];

	// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
	$pathInfo = pathinfo($_SERVER['PHP_SELF']);

	// output: localhost
	$hostName = $_SERVER['HTTP_HOST'];

	// output: http://
	$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

	// return: http://localhost/myproject/
	//return $protocol . $hostName . $pathInfo['dirname'] . "/";
	return $_SERVER['REQUEST_URI'];
}

function fw_util_buildUrl($s_controllerAlias, $s_parentControllerAlias, $b_prettyUrl, $b_isSubApplication)
{
	include C_FW_customPackage . 'configs/applications.php';
	$s_url = '#';
	//pretty urls are activated so only controller alias to be appended
	if($b_prettyUrl == true) {
		$s_url = fw_util_getAbsoluteUrl() . $s_appRequestVarName . '/' . $s_controllerAlias;
	} else {
		//pretty urls are deactivated
		if($b_isSubApplication == true){
			//sub navigation
			//add parameters for method calls to the parent application controller
			$s_url	= fw_util_getAbsoluteUrl()
					. $s_rootFile 
					. '?' 
					. $s_appRequestVarName 
					. '=' 
					. $s_parentControllerAlias 
					. '&'
					. $s_subAppRequestVarName
					. '='
					. $s_controllerAlias;
		}else{
			//parent application main navigation
			//the $s_appRequestVarName = the $_GET[varname] in the routing class
			$s_url = $this->s_currUrl . $s_rootFile . '?' . $s_appRequestVarName . '=' . $s_controllerAlias;
		}
	}
	
	return $s_url;
}

function fw_util_getAbsoluteUrl() {
	// output: /myproject/index.php
    $currentPath = $_SERVER['PHP_SELF']; 
 
    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
    $pathInfo = pathinfo($_SERVER['PHP_SELF']); 
 
    // output: localhost
    $hostName = $_SERVER['HTTP_HOST']; 
 
    // output: http://
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
 
    // return: http://localhost/myproject/
    return $protocol . $hostName . $pathInfo['dirname'] . "/";
}

function fw_util_redirect($s_appIdent)
{
	$arr_appConf = Claycode_configuration::getInstance()->getAppConfByIdent($s_appIdent);
	//get the initial url now
	$s_url = fw_util_getAbsoluteUrl() . Claycode_configuration::getInstance()->getApplicationRequestVarName() . '/';
	$s_url .= $arr_appConf['controller_class_alias'];
	
	
	header('location:' . $s_url);
}
	
	
?>